﻿using System;
using System.Collections;
using UnityEngine;

public class USTimelineEvent : USTimelineBase
{
    public void AddNewEvent(USEventBase sequencerEvent)
    {
        sequencerEvent.transform.parent = base.transform;
        this.SortEvents();
    }

    public USEventBase Event(int index)
    {
        if (index >= this.EventCount)
        {
            Debug.LogError("超出事件列表的范围");
            return null;
        }
        USEventBase component = base.transform.GetChild(index).GetComponent<USEventBase>();
        if (component == null)
        {
            Debug.LogWarning("时间线"+base.name+"缺少USEventBase组件");
        }
        return component;
    }

    public override void ManuallySetTime(float sequencerTime)
    {
        IEnumerator enumerator = base.transform.GetEnumerator();
        try
        {
            while (enumerator.MoveNext())
            {
                USEventBase component = ((Transform) enumerator.Current).GetComponent<USEventBase>();
                if (component != null)
                {
                    float deltaTime = sequencerTime - component.Firetime;
                    if (component.AffectedObject != null)
                    {
                        component.ManuallySetTime(deltaTime);
                    }
                }
            }
        }
        finally
        {
            IDisposable disposable = enumerator as IDisposable;
            if (disposable != null)
            {
             	disposable.Dispose();
            }
        }
    }

    public override void PauseTimeline()
    {
        foreach (USEventBase base2 in this.Events)
        {
            base2.PauseEvent();
        }
    }

    public override void Process(float sequencerTime)
    {
        float elapsedTime = base.elapsedTime;
        base.elapsedTime = sequencerTime;
        IEnumerator enumerator = base.transform.GetEnumerator();
        try
        {
            while (enumerator.MoveNext())
            {
                USEventBase component = ((Transform) enumerator.Current).GetComponent<USEventBase>();
                if (component != null)
                {
                    if (((elapsedTime < component.Firetime) || (elapsedTime <= 0f)) && (((base.elapsedTime >= component.Firetime) && base.Sequence.IsPlaying) && (component.AffectedObject != null)))
                    {
                        component.FireEvent();
                    }
                    if ((base.elapsedTime > component.Firetime) && (base.elapsedTime <= (component.Firetime + component.Duration)))
                    {
                        float runningTime = sequencerTime - component.Firetime;
                        if (component.AffectedObject != null)
                        {
                            component.ProcessEvent(runningTime);
                        }
                    }
                    if (((elapsedTime <= (component.Firetime + component.Duration)) && (base.elapsedTime > (component.Firetime + component.Duration))) && (component.AffectedObject != null))
                    {
                        component.EndEvent();
                    }
                    if (((elapsedTime >= component.Firetime) && (base.elapsedTime < component.Firetime)) && (component.AffectedObject != null))
                    {
                        component.UndoEvent();
                    }
                }
            }
        }
        finally
        {
            IDisposable disposable = enumerator as IDisposable;
            if (disposable != null)
            {
            	disposable.Dispose();
            }
        }
    }

    public void RemoveAndDestroyEvent(Transform sequencerEvent)
    {
        if (!sequencerEvent.IsChildOf(base.transform))
        {
            Debug.LogError("错误：删除一个不属于该时间线的事件");
        }
        else
        {
            UnityEngine.Object.DestroyImmediate(sequencerEvent.gameObject);
        }
    }

    public override void ResumeTimeline()
    {
        foreach (USEventBase base2 in this.Events)
        {
            if ((!base2.IsFireAndForgetEvent && (base.Sequence.RunningTime > base2.Firetime)) && (base.Sequence.RunningTime < (base2.Firetime + base2.Duration)))
            {
                base2.ResumeEvent();
            }
        }
    }

    public void SortEvents()
    {
        Debug.LogWarning("排序算法未实现!");
    }

    public override void StopTimeline()
    {
        base.elapsedTime = 0f;
        foreach (USEventBase base2 in this.Events)
        {
            base2.StopEvent();
        }
    }

    public int EventCount
    {
        get
        {
            return base.transform.childCount;
        }
    }

    public USEventBase[] Events
    {
        get
        {
            return base.GetComponentsInChildren<USEventBase>();
        }
    }
}

